<?php



// +----------------------------------------------------------------------

// | EasyAdmin

// +----------------------------------------------------------------------

// | PHP交流群: 763822524

// +----------------------------------------------------------------------

// | 开源协议  https://mit-license.org

// +----------------------------------------------------------------------

// | github开源项目：https://github.com/zhongshaofa/EasyAdmin

// +----------------------------------------------------------------------

namespace app\api\controller;

use app\api\common\controller\Api;

use think\facade\Db;

/**

 * Class AdminController

 * @package app\common\controller

 */

class Refund extends Api

{

    public function _structure(){

        if($this->admin_id == 0){

            //$this->result('请先登录',[],400);

        }

    }

   

    /**

     * @desc 校验服务码

     * @access

     * Author: attacker

     * Date: 2018/12/24 下午1:10

     */

    public function order()

    { 

        $order_id = $this->request::param('order_id');

        $refundMoney = $this->request::param('ramount');

        $flow_id = Db::name('wechat_payment') -> where('orders_id',$order_id)->where('status',1) -> value('flow_id');

        $refundNo = createSerialNumber();

        $order = Db::name('order') -> where('id',$order_id) -> field('price,amount') -> findOrEmpty();

        $amount = $order['price'] + $order['amount'];

        $alreadyRefundMoney = Db::name('refund') -> where('orders_id',$order_id) -> sum('amount');

        if(($amount - $alreadyRefundMoney) < $refundMoney){

            $this->result('请输入正确的退款金额',[],200);

        }

        

        $data['flow_id'] = $refundNo;

        $data['amount'] = $refundMoney;

        $data['created_at'] = date('Y-m-d H:i:s',time());

        $data['orders_id'] = $order_id;

        $data['status'] = 1;
        Db::name('order') -> where('id',$order_id)->update(['refund_time'=>time()]);
        Db::name('refund') -> insertGetId($data);
        
        try{

            

            $result = $this->payApp->refund->byOutTradeNumber($flow_id, $refundNo,$amount*100,$refundMoney*100, [

            // 可在此处传入其他参数，详细参数见微信支付文档

                

                'notify_url' => 'https://tree.chemeijia.com/api/wx/refund'

                

            ]);

            $this->result('退款成功',[],200);

            

        }catch(Exception $e){

            halt($e->getMessage());

        }

        $this->result('退款失败',[],400);

        /*$service_code = $this->request::param('serviceCode');

        

        $order = Db::name('order') -> where('code',$service_code) -> where('orders_status',1) -> findOrEmpty(); 

        if(count($order) == 0){

            $this->result('该订单不存在',[],400);

        }

        Db::startTrans();

        try{

            Db::name('order') -> where('id',$order['id']) -> update(['orders_status'=>2,'writeoff_time' => time()]);

            

            Db::commit();



        }catch(Exception $e){

            Db::rollback();

            $this->result('核销失败',[],400);



        }

        $this->result('核销成功',$order['id'],200);*/



        

    }

    public function test()

    { 

        $order_id = $this->request::param('order_id');

        $refundMoney = $this->request::param('ramount');

        $flow_id = Db::name('wechat_payment') -> where('orders_id',$order_id)->where('status',1) -> value('flow_id');

        //$refundNo = createSerialNumber();

        $order = Db::name('order') -> where('id',$order_id) -> field('price,amount') -> findOrEmpty();

        $amount = $order['price'] + $order['amount'];

        $alreadyRefundMoney = Db::name('refund') -> where('orders_id',$order_id) -> sum('amount');

        if(($amount - $alreadyRefundMoney) < $refundMoney){

            $this->result('请输入正确的退款金额',[],200);

        }

        

        //$data['flow_id'] = $refundNo;

        $data['amount'] = $refundMoney;

        $data['created_at'] = date('Y-m-d H:i:s',time());

        $data['orders_id'] = $order_id;

        $data['status'] = 1;

        $refundNo = "120230503770";

        //$refundNo = "120230429615";

        //Db::name('refund') -> insertGetId($data);

        try{

            

            $result = $this->payApp->refund->byOutTradeNumber($flow_id, $refundNo,$amount*100,$refundMoney*100, [

            // 可在此处传入其他参数，详细参数见微信支付文档

                

                'notify_url' => 'https://tree.chemeijia.com/api/wx/refund'

                

            ]);

            $this->result('退款成功',[],200);

            

        }catch(Exception $e){

            halt($e->getMessage());

        }

        $this->result('退款失败',[],400);

        /*$service_code = $this->request::param('serviceCode');

        

        $order = Db::name('order') -> where('code',$service_code) -> where('orders_status',1) -> findOrEmpty(); 

        if(count($order) == 0){

            $this->result('该订单不存在',[],400);

        }

        Db::startTrans();

        try{

            Db::name('order') -> where('id',$order['id']) -> update(['orders_status'=>2,'writeoff_time' => time()]);

            

            Db::commit();



        }catch(Exception $e){

            Db::rollback();

            $this->result('核销失败',[],400);



        }

        $this->result('核销成功',$order['id'],200);*/



        

    }

    



   



}